package com.heima.behavior.service.impl;

import com.heima.behavior.model.dto.UnLikesBehaviorDto;
import com.heima.behavior.model.pojo.ApLikesBehavior;
import com.heima.behavior.model.pojo.ApUnlikesBehavior;
import com.heima.behavior.service.ApUnlikesBehaviorService;
import com.heima.behavior.utils.AppThreadLocalUtil;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ApUnlikesBehaviorServiceImpl implements ApUnlikesBehaviorService {

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 不喜欢
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unlikesBehavior(UnLikesBehaviorDto dto) {
        //1.参数检验
        if (dto == null || dto.getArticleId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        Long userId = AppThreadLocalUtil.getId();
        if (userId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //2.查询用户是否已经点击过不喜欢
        String unlike_hash_key = "USER_UNLIKES_" + dto.getArticleId();
        Boolean hasKey = stringRedisTemplate.opsForHash().hasKey(unlike_hash_key, userId + "");
        //3.1已点不喜欢-- 不喜欢
        if (hasKey && dto.getType() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
        }
        //3.2已点不喜欢-- 取消不喜欢
        if (hasKey && dto.getType() == 1) {
            //3.2.1删除redis
            stringRedisTemplate.opsForHash().delete(unlike_hash_key, userId + "");
            //3.2.2删除数据库数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("articleId").is(dto.getArticleId()));
            mongoTemplate.remove(query, ApUnlikesBehavior.class);
            return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
        }
        //3.3未点不喜欢 --不喜欢
        if (!hasKey && dto.getType() == 0) {
            //3.3.1保存数据到redis
            stringRedisTemplate.opsForHash().put(unlike_hash_key, userId + "", "1");
            //3.3.2保存数据到mongodb
            ApUnlikesBehavior apUnlikesBehavior = new ApUnlikesBehavior();
            apUnlikesBehavior.setArticleId(dto.getArticleId());
            apUnlikesBehavior.setCreatedTime(new Date());
            apUnlikesBehavior.setType(dto.getType());
            apUnlikesBehavior.setUserId(userId);
            apUnlikesBehavior.setId(ObjectId.get().toHexString());
            mongoTemplate.save(apUnlikesBehavior);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }
}
